package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQueryObject;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;


    @Override
    public TablePageInfo<Statement> query(StatementQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        //查询的时候多加一个数据,是否被软删除了
        List<Statement> statements = statementMapper.selectForList(qo);

        return new TablePageInfo<Statement>(statements);
    }
    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void update(Statement statement) {
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override//把预约单的信息转换成结算单的样子
    public Statement getByAppointmentId(Long appointmentId) {
        return statementMapper.selectByAppointment(appointmentId);
    }




    @Override
    public Statement generateStatement(Long appointmentId) {
        appointmentMapper.updateByStart(appointmentId,Appointment.STATUS_SETTLE);

        Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
        Statement statement = new Statement();

        String customerName = appointment.getCustomerName();
        statement.setCustomerName(customerName);

        String customerPhone = appointment.getCustomerPhone();
        statement.setCustomerPhone(customerPhone);
        //到店时间
        Date actualArrivalTime = appointment.getActualArrivalTime();
        statement.setActualArrivalTime(actualArrivalTime);

        //车牌
        String licensePlate = appointment.getLicensePlate();
        statement.setLicensePlate(licensePlate);

        //汽车类型
        String carSeries = appointment.getCarSeries();
        statement.setCarSeries(carSeries);

        //服务项目类型,保养维修等
        Integer serviceType = appointment.getServiceType();
        statement.setServiceType(serviceType);

        //预约单id
        Long appointmentId1 = appointment.getId();
        statement.setAppointmentId(appointmentId);
        //状态消费中

        //支付时间 暂无

        //处理人id

        //总金额

        //创建时间
        statement.setCreateTime(new Date());

        //备注
        String info = appointment.getInfo();

        //保存到数据库中
        statementMapper.insert(statement);
        return statement;
    }

    @Override
    public void updateByStart(Long statementId) {
        statementMapper.updateByStart(statementId,Statement.STATUS_PAID);
    }

    @Override
    public Statement getStatement(Long statementId) {
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        Long userId = ShiroUtils.getUserId();//当前登录人id
        //收款人
        User user = ShiroUtils.getUser();
        statement.setPayee(user);

        statement.setPayeeId(userId);
        //收款时间
        statement.setPayTime(new Date());
        //如果是预约客户还要设置预约时间
        statementMapper.updateByStatement2(statement);
        return statement;
    }

//    @Override
//    public Statement getShowDetail(Long statementId) {
//        statementId.
//        User user = ShiroUtils.getUser();
//        statement.setPayee(user);
//        return statement;
//    }


    @Override//保存
    public void save(Statement statement) {
        statement.setCreateTime(new Date());
        //新的预约单给默认状态预约中
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }




    @Override//删除
    public void deleteBatch(Long ids) {
        //删除预约表
        Statement statement = statementMapper.selectByPrimaryKey(ids);
        Long appointmentId = statement.getAppointmentId();
        if (appointmentId!=null){
            appointmentMapper.deleteByPrimaryKey(appointmentId);
        }
        //删除服务客户关系表
        statementItemMapper.deleteByStartment(ids);
            //硬删除
            statementMapper.deleteByPrimaryKey(ids);
    }
}
